﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Users;
using iOak.Business.Resumes;
using iOak.Domain.Resumes;
using iOak.Domain.Users;
using iOak.Business.Authentication;

namespace iOak.Web
{
    public partial class CompanyRegister : System.Web.UI.Page
    {
        CompanyService _companyService = new CompanyService();
        UserService _userService = new UserService();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRegist_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                Company company = new Company();
                company.CompanyId = Guid.NewGuid();
                company.CompanyName = txtCompanyName.Text.Trim();
                company.ContactPerson = txtContactPerson.Text.Trim();
                company.ContactPhone = txtContactPhone.Text.Trim();
                company.CreatedBy = Guid.Empty;
                company.CreatedOn = DateTime.Now;
                company.ModifiedBy = Guid.Empty;
                company.ModifiedOn = DateTime.Now;
                company.IsActive = true;
                _companyService.Create(company);

                var user = new User
                {
                    UserId = Guid.NewGuid(),
                    PasswordHash = txtPassword.Text.Trim(),
                    FullName = company.ContactPerson,
                    CompanyId = company.CompanyId,
                    UserName = txtUserName.Text.Trim(),
                    Active = true
                };

                _userService.RegisterUser(user);

                var authenticationService = new FormsAuthenticationService();

                // sign in user
                authenticationService.SignIn(user, false);

                this.Response.Redirect("~/CompanyEdit.aspx");
            }
        }

        /// <summary>
        /// 数据验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            String message = String.Empty;
            var company = _companyService.GetByName(txtCompanyName.Text.Trim());
            if (company != null)
            {
                message = "输入的公司名已存在!";
            }
            else
            {
                var user = _userService.GetUserByName(txtUserName.Text.Trim());
                if (user != null)
                {
                    message = "输入的用户名已存在!";
                }
            }
            if (!String.IsNullOrWhiteSpace(message))
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "showWarnning", "$(document).ready(function(){jAlert('" + message + "');});", true);
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}